package com.pokermadness.chat;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Chat implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7574780858629469066L;
	ArrayList<Message> messages;
	
	public Chat() {
		messages = new ArrayList<Message>();
	}
	
	private void addMessage(String message) {
		messages.add(new Message(6, message));
	}
	
	public void processMessage(String message) {
		addMessage(message);
		addResponse();
	}
	
	public JSONObject getRandomMessage() {
		addResponse();
		return getResponse();
	}
	
	public JSONObject getResponse() {
		JSONObject response = new JSONObject();
		try {
			response.put("author", getLast().author);
			response.put("content", getLast().content);
		} catch (JSONException e) {
			// do something
		}
		return response;
	}
	
	public JSONArray getAllMessages() {
		JSONArray array = new JSONArray();
		JSONObject temp;
		for(int i = 0; i < messages.size(); i++) {
			temp = new JSONObject();
			try {
				temp.put("author", messages.get(i).author);
				temp.put("content", messages.get(i).content);
				array.put(temp);
			} catch (JSONException e) {
				// do something
			}
		}
		return array;
	}

	private void addResponse() {
		int random;
		String reply;
		int gamesplayed = (int) Math.floor(Math.random() * 12 + 1);
		String[] replies = {"Nice day, isn't it?", "You are bluffing.", 
		               "You will be cleaned soon.", "This is my lucky day.",
		               "Your luck will soon run out.", "What's up?",
		               "Don't be afraid to raise :)", "I can see right through your plans.",
		               "Do you feel lucky?", "Easy money for me!", "Give up and I'll save you from embarrassment.",
		               "Your chips will be mine.", "You think you are professional?", "Are you ready to lose your money?",
		               "Nothing beats a good poker game, does it?", "I bet you lose today :P", 
		               "I heard you have tendency to lose :D", "You play like an amateur.",
		               "I have seen better players.", "I'm totally going to beat you.", "Is this your first time?",
		               "I recommend you to read some poker rules before playing with us.",
		               "Can't handle my playing style?", "Where are you from?", 
		               "I have once played on World Series of Poker.",
		               "I have won my last " + gamesplayed +" games in a row.",
		               "Even my dog can play better than you."
						};
		random = (int) Math.floor(Math.random() * replies.length);
		reply = replies[random];
		do {
			random = (int) Math.floor(Math.random() * 9 + 1);
		} while(random == 6);
		
		messages.add(new Message(random, reply));
	}
	
	private Message getLast() {
		return messages.get(messages.size() - 1);
	}
	
	private class Message implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = -4386383672272372613L;
		private int author;
		private String content;
		
		public Message(int author, String content) {
			this.author = author;
			this.content = content;
		}
	}

}
